﻿using Org.BouncyCastle.Crypto.Engines;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Textbook
{
    public partial class TeaForm : Form
    {
        public TeaForm(Home home, Teacher teacher)
        {
            InitializeComponent();
            this.home = home;
            this.teacher = teacher;
            getBooks();
        }

        Home home;

        Teacher teacher;

        List<Book> list;

        SQL sql = new SQL();

        private void getBooks()
        {
            list = sql.getBookList();
            name.Text = list[0].B_name.ToString();
            price.Text = list[0].B_price.ToString();
            name.DataSource = list;
            name.DisplayMember = "b_name";
            name.ValueMember = "b_price";
        }

        bool e1 = true;
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 0)
            {
                //购书单
                getBooks();

            }
            else if (e.TabPageIndex == 1)
            {
                //领书单
                DataSet ds = sql.selectBillById(teacher.T_id, false, 1);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    if (e1)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "领 书";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView1.Columns.Add(column);
                        e1 = false;
                    }
                }
            }
            else
            {
                if (MessageBox.Show("确定要退出...", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    home.openForm(new Login(home));
                }
            }
        }

        bool f = true;
        private void name_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (f)
            {
                f = false;
                return;
            }
            price.Text = name.SelectedValue.ToString();
        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (num.Text == "")
            {
                MessageBox.Show("请填写数量...");
            }
            else
            {
                Bill bill = new Bill();
                bill.B_id = Utils.getTimeTicks();
                bill.T_id = teacher.T_id;
                foreach (var item in list)
                {
                    if (item.B_price == decimal.Parse(price.Text) && item.B_name == name.Text)
                    {
                        bill.Book_id = item.Book_id;
                        break;
                    }
                }
                bill.B_time = DateTime.Parse(time.Text).ToString("yyyy-MM-dd");
                decimal p = decimal.Parse(price.Text) * int.Parse(num.Text);
                bill.B_money = decimal.Parse(p.ToString("0.00"));
                bill.B_num = int.Parse(num.Text);
                bill.S_id = "";
                if (sql.insertBill(bill))
                {
                    MessageBox.Show("订购成功...");
                }
                else
                {
                    MessageBox.Show("订购失败...");
                }
            }
        }

        private void num_TextChanged(object sender, EventArgs e)
        {
            if (num.Text[num.Text.Length - 1] >= '0' && num.Text[num.Text.Length - 1] <= '9')
            {
                if (num.Text == "")
                {
                    return;
                }
                if (long.Parse(num.Text) > 10000)
                {
                    MessageBox.Show("一次最多只能订购10000本");
                    return;
                }
            }
            else
            {
                num.Text = num.Text.Remove(num.Text.Length - 1);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 &&
                MessageBox.Show("确定领书？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //领书
                string b_id = dataGridView1.Rows[e.RowIndex].Cells["订单号"].Value.ToString();
                int book_id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["书籍编号"].Value.ToString());
                int num = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["数量"].Value.ToString());
                if (sql.updateBillandBook(b_id, book_id, num, -1))
                {
                    MessageBox.Show("书籍领取成功...");
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("订购失败...");
                }
            }
        }
    }
}
